package com.fingard.dsp.bank.directbank.boc01;


import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.ICodeFile;
import com.fingard.dsp.bank.reqrespobj.downloadcodefile.ReqQryCodeFile;
import com.fingard.dsp.bank.reqrespobj.downloadcodefile.RetAtsCodeFile;
import com.fingard.io.FileHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


public class BOC01CodeFile extends BOC01Base implements ICodeFile {
    @Override
    public RetAtsCodeFile downloadCodeFile(ReqQryCodeFile p_reqCodeFile) {
        RetAtsCodeFile retAtsCodeFile=new RetAtsCodeFile(p_reqCodeFile.reqHeader);
        try{
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0043";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0043-rq");
            xmlWriter.writeStartElement("b2e0043-rq");
            xmlWriter.writeElementString("tasktpy", p_reqCodeFile.fileType);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            //请求
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            //测试
            //WriteBankLogStep2(xmlWriter.toXmlString());
            //String[] tmpStrRet=new String[]{"",FileHelper.readFileText("D:/test/123.xml","utf-8")};
            //WriteBankLogStep3(tmpStrRet[1]);

            //处理响应
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String rspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0043-rs/status/rspcod");//响应码
                    String rspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0043-rs/status/rspmsg");//响应信息
                    if (isActiveRetCode(rspcod)) {
                        retAtsCodeFile.respCode = "0";
                        retAtsCodeFile.respInfo = rspmsg;
                        //生成临时文件，存放行号更新文件
                        String localFilePath=FGBiz.dspCenter.dspSet.codeMainDir+"/"+"b2e0043"+"/"+p_reqCodeFile.fileType+"_"+p_reqCodeFile.dateTime+".txt";
                        FileHelper.writeFile(localFilePath,p_reqCodeFile.fileType+"_"+p_reqCodeFile.dateTime,getCharset());
                    }else {
                        retAtsCodeFile.respCode = "-1";
                        retAtsCodeFile.respInfo = rspmsg;
                    }
                }else {
                    retAtsCodeFile.respCode = "-1";
                    retAtsCodeFile.respInfo = tmpSbMsg.toString();
                }
            }else {
                retAtsCodeFile.respCode = "-1";
                retAtsCodeFile.respInfo = tmpStrRet[1];
            }
        }catch (Exception e){
            e.printStackTrace();
            WriteBankLogLn("行号更新文件下载异常:"+e.getMessage());
        }
        retAtsCodeFile.writeRetToATS();
        return retAtsCodeFile;
    }
}
